package com.eian.boot.common.core.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 执行堆栈工具类
 *
 * @author alex.meng
 * @createTime 2024-09-03 02:48
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StackUtil {

    /**
     * 获取调用者
     *
     * @param currentClassName 当前类名
     * @return 调用者
     */
    public static String getCallingMethod(String currentClassName) {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        for (StackTraceElement element : stackTraceElements) {
            if (element.getClassName().startsWith("com.eian.")
                    && !element.getClassName().equals(StackUtil.class.getName())
                    && !element.getClassName().equals(currentClassName)) {
                int lastDotIndex = element.getClassName().lastIndexOf('.');
                String className = element.getClassName().substring(element.getClassName().lastIndexOf('.', lastDotIndex) + 1);
                return className + "." + element.getMethodName() + ":" + element.getLineNumber();
            }
        }
        return "未找到调用者";
    }
}
